﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for QuangCaoProvider
/// CuongBH
/// 07/12/2011
/// </summary>
namespace EHTWebsite.Code
{
    public class QuangCaoProvider
    {
        public static Int32 IUQuangCao(QuangCao theQuangCao, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_IUBanner]";					
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theSanPhamParameter = new SqlParameter();

            theSanPhamParameter.ParameterName = "@BannerIDOut";
            theSanPhamParameter.Direction = ParameterDirection.Output;
            theSanPhamParameter.DbType = DbType.Int32;

            command.Parameters.Add(theSanPhamParameter);
            command.Parameters.Add(new SqlParameter("@BannerID", theQuangCao.BannerID));
            command.Parameters.Add(new SqlParameter("@BannerUrl", theQuangCao.BannerUrl));
            command.Parameters.Add(new SqlParameter("@PhanVungID", theQuangCao.PhanVungID));
            command.Parameters.Add(new SqlParameter("@GhiChuVN",theQuangCao.GhiChuVN));
            command.Parameters.Add(new SqlParameter("@GhiChuOther", theQuangCao.GhiChuOther));           
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theQuangCao.NumberOrder));
            command.Parameters.Add(new SqlParameter("@TypeBanner", theQuangCao.TypeBanner));
            command.Parameters.Add(new SqlParameter("@Url", theQuangCao.Url));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
            return Int32.Parse(command.Parameters["@BannerIDOut"].Value.ToString());
        }
        public static void DeleteQuangCaoByQuangCaoID(Int32 BannerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteBannerByBannerID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", BannerID));

            DataProvider.ExecuteNoneQuery(command);
        }

        public static DataSet GetQuangCaos(Int32 TypeBanner, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetBannersForBackEnd]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@TypeBanner", TypeBanner));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);

            return ds;
        }
        public static QuangCao GetQuangCaoByQuangCaoID(Int32 BannerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "[proc_GetBannerByBannerID]";
            command.Parameters.Add(new SqlParameter("@BannerID", BannerID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            QuangCao theQuangCao = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theQuangCao = new QuangCao();
                theQuangCao.BannerID = Int32.Parse(ds.Tables[0].Rows[0]["BannerID"].ToString());
                theQuangCao.BannerUrl = ds.Tables[0].Rows[0]["BannerUrl"].ToString();
                theQuangCao.GhiChuOther = ds.Tables[0].Rows[0]["GhiChuOther"].ToString();
                theQuangCao.GhiChuVN = ds.Tables[0].Rows[0]["GhiChuVN"].ToString();
                theQuangCao.NumberOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theQuangCao.PhanVungID = Int32.Parse(ds.Tables[0].Rows[0]["PhanVungID"].ToString());
                theQuangCao.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theQuangCao.TypeBanner = ds.Tables[0].Rows[0]["TypeBanner"].ToString();
                theQuangCao.Url = ds.Tables[0].Rows[0]["Url"].ToString();
            }

            return theQuangCao;
        }

        public static void ChangePublish(Int32 BannerID, Boolean TrangThai)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishByBannerID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BannerID", BannerID));
            command.Parameters.Add(new SqlParameter("@Publish", TrangThai));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}